//
//  OYWelcomeController.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/4.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit
import SDWebImage

class OYWelcomeController: UIViewController {

    override func loadView() {
        
        super.loadView()
        let backGroundView: UIImageView = UIImageView(frame: self.view.bounds)
        backGroundView.image = UIImage(named: "ad_background")
        
        self.view = backGroundView
        
    }
    
    lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        if let imageUrl = OYUserInfoViewModel.sharedUserInfo.userInfo?.avatar_large {
            iconView.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named:"avatar_default_big"))
        }
        
        return iconView
        
    }()
    
    lazy var nameLabel:UILabel = {
        let nameLabel = UILabel()
        if let userName = OYUserInfoViewModel.sharedUserInfo.userInfo?.screen_name{
            nameLabel.text = "欢迎回来，\(userName)"
        }else {
            nameLabel.text = "欢迎回来"
        }
        nameLabel.alpha = 0
        return nameLabel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    func setupUI() {
        
        // 1.创建一个圆角的iconView头像视图90 * 90
        view.addSubview(iconView)
        // 2.创建一个label，内容是用户昵称
        view.addSubview(nameLabel)
        // 3.对两个控件进行约束布局
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(10)
        }
     
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        iconAnimation()
    }
    
    func iconAnimation() {
        iconView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //  usingSpringWithDamping 0 - 1 取值范围
        UIView.animate(withDuration: 1, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            
        }, completion: {(_) -> Void in
            
            UIView.animate(withDuration: 1, animations: {
                //  设置透明度
                self.nameLabel.alpha = 1
            }, completion: { (_) in
                //  切换到主页视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootViewController), object: nil)
            })
            
        })
        
    }
    
    
}
